package com.css.zfzx.sjcj.modules.hddcwyGeophySvyLine.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeophySvyLine.repository.HddcWyGeophysvylineNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeophySvyLine.repository.HddcWyGeophysvylineRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeophySvyLine.repository.entity.HddcWyGeophysvylineEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeophySvyLine.service.HddcWyGeophysvylineService;
import com.css.zfzx.sjcj.modules.hddcwyGeophySvyLine.viewobjects.HddcWyGeophysvylineQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeophySvyLine.viewobjects.HddcWyGeophysvylineVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-01
 */
@Service
public class HddcWyGeophysvylineServiceImpl implements HddcWyGeophysvylineService {

	@Autowired
    private HddcWyGeophysvylineRepository hddcWyGeophysvylineRepository;
    @Autowired
    private HddcWyGeophysvylineNativeRepository hddcWyGeophysvylineNativeRepository;

    @Override
    public JSONObject queryHddcWyGeophysvylines(HddcWyGeophysvylineQueryParams queryParams , int curPage,
                                                int pageSize) {
        Page<HddcWyGeophysvylineEntity> hddcWyGeophysvylinePage =
                this.hddcWyGeophysvylineNativeRepository.queryHddcWyGeophysvylines(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeophysvylinePage);
        return jsonObject;
    }


    @Override
    public HddcWyGeophysvylineEntity getHddcWyGeophysvyline(String id) {
        HddcWyGeophysvylineEntity hddcWyGeophysvyline = this.hddcWyGeophysvylineRepository.findById(id).orElse(null);
         return hddcWyGeophysvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeophysvylineEntity saveHddcWyGeophysvyline(HddcWyGeophysvylineEntity hddcWyGeophysvyline) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeophysvyline.setId(uuid);
        hddcWyGeophysvyline.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeophysvyline.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeophysvylineRepository.save(hddcWyGeophysvyline);
        return hddcWyGeophysvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeophysvylineEntity updateHddcWyGeophysvyline(HddcWyGeophysvylineEntity hddcWyGeophysvyline) {
        hddcWyGeophysvyline.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeophysvyline.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeophysvylineRepository.save(hddcWyGeophysvyline);
        return hddcWyGeophysvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeophysvylines(List<String> ids) {
        List<HddcWyGeophysvylineEntity> hddcWyGeophysvylineList = this.hddcWyGeophysvylineRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeophysvylineList) && hddcWyGeophysvylineList.size() > 0) {
            for(HddcWyGeophysvylineEntity hddcWyGeophysvyline : hddcWyGeophysvylineList) {
                this.hddcWyGeophysvylineRepository.delete(hddcWyGeophysvyline);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
